<?php
require_once ('..' . DIRECTORY_SEPARATOR . 'initialize.php');
include_once(Utils::getPath(array('shared', 'template.php')));
include_once(Utils::getPath(array('repositories', 'data.php')));
include_once(Utils::getPath(array('repositories', 'userRepository.php')));
include_once(Utils::getPath(array('repositories', 'challengeRepository.php')));
include_once(Utils::getPath(array('repositories', 'solutionRepository.php')));
include_once(Utils::getPath(array('repositories', 'commentRepository.php')));
include_once(Utils::getPath(array('summary.php')));

$template = new Template('application');
$template->initialize();

if (isset($_GET['id']))
    $iduser = $_GET['id'];
else
    $iduser = $_SESSION['authenticatedId'];

if (!intval($iduser)) {
    Utils::goToErrorPage(StatusCodes::HTTP_BAD_REQUEST);
}

$userRep = new UserRepository();
$user = $userRep->getById($iduser);
$referer = getenv("HTTP_REFERER");

if (!isset($user) || $user->getIsDeleted()) {
    Utils::goToErrorPage();
} else if ($_SESSION['authenticatedId'] == $iduser) {
    header('Location: ' . Utils::getUrl('profile.php'));
    exit();
}

$state = $userRep->getFriendsState($_SESSION['authenticatedId'], $iduser);

$username = $user->getUserName();
$name = $user->getName();
$email = $user->getEmail();
$image = $user->getImage();
$description = $user->getDescription();

$news = Summary::getProfile($_SESSION['authenticatedId'], $user->getId());

if (!is_null($state)) {
    if ($state[0])
        $stateMessage = 'FRIEND - <a href="requests.php?delete=' . $iduser . '">Delete</a>';
    else {
        $stateMessage = 'PENDING';
        if ($state[1])
            $stateMessage .= '- <a href="requests.php?add=' . $iduser . '">Accept</a>';
    }
}else
    $stateMessage = '<a href="requests.php?add=' . $iduser . '">Send invitation</a>';

//Get Statistics
$chaRep = new ChallengeRepository();
$lastChaA = $chaRep->findByAuthor($iduser, 'Date', true, 0, 1);
if (count($lastChaA) > 0) {
    $lastCha = $lastChaA[0];
    $countCha = $chaRep->countLast();
}
$solRep = new SolutionRepository();
$lastSolA = $solRep->findByAuthor($iduser, true, 'Date', true, 0, 1);
if (count($lastSolA) > 0) {
    $lastSol = $lastSolA[0];
    $countSol = $solRep->countLast();
    $sumQuality = $solRep->sumQuality();
}

$comRep = new CommentRepository();
$lastComA = $comRep->findByAuthor($iduser, null, 'Date', true, 0, 1);
if (count($lastComA) > 0) {
    $lastCom = $lastComA[0];
    $countCom = $comRep->countLast();
}


?>
    <h2><?php echo $username; ?></h2>
<div id="profile-information">
    <div id="profile-avatar"><img alt="User Avatar" src ="<?php echo $image; ?>" /></div>

    <div id="profile-info">
    <div><span>Name: </span><?php echo $name; ?></div>
    <div><span>Email: </span><?php echo $email; ?></div>
    <div><span>Friendship: </span><?php echo $stateMessage; ?></div>
    <div id="profile-send"><a class="button" href="<?php echo Utils::getUrl("message/new.php?id=$iduser") ?>">Send message</a></div>
    </div>
</div>

<div class="profile-description"><p><?php echo $description; ?></p></div>

<div id="profile-statistics">
    <?php if (isset($lastCha)) { ?>
        <div class="statistic">
            <div class="statistic-label"><span>Uploaded challenges:<span></div>
            <div class="statistic-field"><?php echo $countCha ?></div>
        </div>
        <div class="statistic">
            <div class="statistic-label"><span>Last uploaded challenge:</span></div>
            <div class="statistic-field"><a href="<?php echo Utils::getUrl('challenge/view.php?id=' . $lastCha->getId()) ?>"><?php echo Utils::ago($lastCha->getDate()) ?></a></div>
        </div>
    <?php } ?>
    <?php if (isset($lastSol)) { ?>
        <div class="statistic">
            <div class="statistic-label"><span>Uploaded solutions:</span></div>
            <div class="statistic-field"><?php echo $countSol ?></div>
        </div>
        <div class="statistic">
            <div class="statistic-label"><span>Last uploaded solution:</span></div>
            <div class="statistic-field">
                <?php if($solRep->userCanAccessSolution($_SESSION['authenticatedId'], $lastSol)) {?>
                <a href="<?php echo Utils::getUrl('solution/view.php?id=' . $lastSol->getId()) ?>"><?php echo Utils::ago($lastSol->getDate()) ?></a>
                <?php } else { ?>
                <?php echo Utils::ago($lastSol->getDate()) ?>
                <?php } ?>
            </div>
        </div>
        <div class="statistic">
            <div class="statistic-label"><span>Mean quality of solutions:</span></div>
            <div class="statistic-field"><?php echo ($sumQuality / $countSol) ?></div>
        </div>
    <?php } ?>
    <?php if (isset($lastCom)) { ?>
        <div class="statistic">
            <div class="statistic-label"><span>Number of comments:</span></div>
            <div class="statistic-field"><?php echo $countCom ?></div>
        </div>
    <?php } ?>
    </div>



<div id="profile-activity">
    <h3>
        Latest <?php echo $username; ?> activity:
    </h3>
    <div id="news">
        <?php for ($i = 0; $i < count($news); $i++) { ?>
            <div class="new-element">
                <div class="new-text"><?php echo $news[$i]['message'] ?></div>
                <?php if ($news[$i]['url'] != null) { ?>
                    <div class="new-url"><a href="<?php echo $news[$i]['url'] ?>">View</a></div>
                <?php } ?>
                <div class="new-date"><?php echo Utils::ago($news[$i]['datetime']) ?></div>        
            </div>
        <?php } 
        if (count($news) == 0) { ?>
        <div>This user has not had any activity.</div>
        <?php } ?>
    </div>
</div>
<?php
$template->createPage("Profile $username");
?>